<template>
	<view class="goods-item">
		<!-- 左侧盒子 -->
		<view class="goods-item-left">
			<radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"></radio>
			<image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
		</view>
		<!-- 右侧盒子 -->
		<view class="goods-item-right">
			<!-- 商品名字 -->
			<view class="goods-name">{{ goods.goods_name }}</view>
			<view class="godds-info-box">
				<view class="goods-price">¥{{ goods.goods_price | tofixed }}</view>
				<!-- //商品数量 -->
				<uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numberChangeHandler" @blur="numberblurHandler"></uni-number-box>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'my-goods',
	props: {
		goods: {
			type: Object,
			defaul: {}
		},
		showRadio: {
			type: Boolean,
			//默认情况下 ,不会展示redio 组件
			default: false
		},
		showNum: {
			type: Boolean,
			//默认情况下 ,不会展示redio 组件
			default: false
		}
	},
	data() {
		return {
			//默认图片
			defaultPic: 'https://ww1.sinaimg.cn/large/007rAy9hgy1g24by9t530j30i20i2glm.jpg'
		};
	},
	filters: {
		tofixed(num) {
			return Number(num).toFixed(2);
		}
	},
	methods: {
		radioClickHandler() {
			this.$emit('radio-change', {
				goods_id: this.goods.goods_id,
				goods_state: !this.goods.goods_state
			});
		},
		//监听NumberBox 组件数量变化的事件
		numberChangeHandler(val) {
			this.$emit('num-change', {
				goods_id: this.goods.goods_id,
				goods_count: val - 0
			});
		},
		//商品几件的输入框失去焦点时触发
		numberblurHandler(e){
		    // console.log(e)
		    let old=this.goods.goods_count
		    //4舍5入取整
		    let value=parseInt(e.detail.value)
		    // console.log(value);
		    //判断是否为数字
		    if(!isNaN(value)&& typeof value!= 'string'){
		     e.detail.value=value
		    }else{
		     e.detail.value=old
		    }
		   }	
	}
};
</script>

<style lang="scss">
.goods-item {
	display: flex;
	padding: 10px 5px;
	border-bottom: 1px solid #f0f0f0;
	.goods-item-left {
		margin-right: 5px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.goods-pic {
			width: 100px;
			height: 100px;
			display: block;
		}
	}
	.goods-item-right {
		display: flex;
		flex: 1;
		flex-direction: column;
		justify-content: space-between;
		.goods-name {
			font-size: 13px;
		}
		.godds-info-box {
			display: flex;
			justify-content: space-between;

			.goods-price {
				color: #c00000;
				font-size: 16px;
			}
		}
	}
}
</style>
